package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import action.Action;
import model.Program;

import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class SpecificProgramInfo extends JDialog {
	private Program program;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField textField_12;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;

	/**
	 * Launch the application.
	 */
	public static  void showMesDialog(Program p) {
		try {
			SpecificProgramInfo dialog = new SpecificProgramInfo(p);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SpecificProgramInfo(Program p) {
		this.program = p;
		
		setBounds(100, 100, 709, 548);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 669, 417);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("ID:");
			lblNewLabel.setFont(new Font("SimSun", Font.BOLD, 20));
			lblNewLabel.setBounds(10, 20, 85, 22);
			contentPanel.add(lblNewLabel);
		}
		
		JLabel lblCountry = new JLabel("COUNTRY:");
		lblCountry.setFont(new Font("SimSun", Font.BOLD, 20));
		lblCountry.setBounds(10, 79, 112, 22);
		contentPanel.add(lblCountry);
		
		JLabel lblUniversity = new JLabel("UNIVERSITY:");
		lblUniversity.setFont(new Font("SimSun", Font.BOLD, 20));
		lblUniversity.setBounds(10, 133, 134, 22);
		contentPanel.add(lblUniversity);
		
		JLabel lblSchool = new JLabel("SCHOOL:");
		lblSchool.setFont(new Font("SimSun", Font.BOLD, 20));
		lblSchool.setBounds(10, 194, 85, 22);
		contentPanel.add(lblSchool);
		
		JLabel lblProgramName = new JLabel("PROGRAM NAME:");
		lblProgramName.setFont(new Font("SimSun", Font.BOLD, 20));
		lblProgramName.setBounds(10, 255, 158, 22);
		contentPanel.add(lblProgramName);
		
		JLabel lblHomepage = new JLabel("HOMEPAGE:");
		lblHomepage.setFont(new Font("SimSun", Font.BOLD, 20));
		lblHomepage.setBounds(10, 320, 112, 22);
		contentPanel.add(lblHomepage);
		
		textField_12 = new JTextField();
		textField_12.setBounds(56, 23, 243, 21);
		textField_12.setText(program.getId());
		contentPanel.add(textField_12);
		textField_12.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(105, 79, 194, 21);
		textField_1.setText(program.getCountry());
		contentPanel.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(135, 136, 164, 21);
		textField_2.setText(program.getUniversity());
		contentPanel.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(105, 197, 194, 21);
		textField_3.setText(program.getSchool());
		contentPanel.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(154, 258, 145, 21);
		textField_4.setText(program.getProgramName());
		contentPanel.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(135, 323, 164, 21);
		textField_5.setText(program.getHomepage());
		contentPanel.add(textField_5);
		
		JLabel lblLocation = new JLabel("LOCATION:");
		lblLocation.setFont(new Font("SimSun", Font.BOLD, 20));
		lblLocation.setBounds(345, 23, 123, 22);
		contentPanel.add(lblLocation);
		
		JLabel lblEmail = new JLabel("EMAIL:");
		lblEmail.setFont(new Font("SimSun", Font.BOLD, 20));
		lblEmail.setBounds(345, 79, 85, 22);
		contentPanel.add(lblEmail);
		
		JLabel lblPhoneNumber = new JLabel("PHONE NUMBER:");
		lblPhoneNumber.setFont(new Font("SimSun", Font.BOLD, 20));
		lblPhoneNumber.setBounds(345, 133, 158, 22);
		contentPanel.add(lblPhoneNumber);
		
		JLabel lblDegree = new JLabel("DEGREE:");
		lblDegree.setFont(new Font("SimSun", Font.BOLD, 20));
		lblDegree.setBounds(345, 194, 85, 22);
		contentPanel.add(lblDegree);
		
		JLabel lblDdlwithaid = new JLabel("DDLWITHAID:");
		lblDdlwithaid.setFont(new Font("SimSun", Font.BOLD, 20));
		lblDdlwithaid.setBounds(345, 255, 123, 22);
		contentPanel.add(lblDdlwithaid);
		
		JLabel lblDdlwitoutaid = new JLabel("DDLWITOUTAID:");
		lblDdlwitoutaid.setFont(new Font("SimSun", Font.BOLD, 20));
		lblDdlwitoutaid.setBounds(345, 320, 175, 22);
		contentPanel.add(lblDdlwitoutaid);
		
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		textField_6.setBounds(448, 23, 205, 21);
		textField_6.setText(program.getLocation());
		contentPanel.add(textField_6);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(420, 82, 233, 21);
		textField_7.setText(program.getEmail());
		contentPanel.add(textField_7);
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		textField_8.setBounds(489, 136, 164, 21);
		textField_8.setText(program.getPhoneNumber());
		contentPanel.add(textField_8);
		
		textField_9 = new JTextField();
		textField_9.setColumns(10);
		textField_9.setBounds(420, 194, 233, 21);
		textField_9.setText(program.getDegree());
		contentPanel.add(textField_9);
		
		textField_10 = new JTextField();
		textField_10.setColumns(10);
		textField_10.setBounds(478, 258, 175, 21);
		textField_10.setText(program.getDeadlineWithAid());
		contentPanel.add(textField_10);
		
		textField_11 = new JTextField();
		textField_11.setColumns(10);
		textField_11.setBounds(489, 323, 164, 21);
		textField_11.setText(program.getDeadlineWithoutAid());
		contentPanel.add(textField_11);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBounds(0, 427, 693, 82);
			getContentPane().add(buttonPane);
			buttonPane.setLayout(null);
			{
				JButton updateButton = new JButton("Update");
				updateButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(textField_1.getText().isEmpty()||textField_2.getText().isEmpty()||textField_3.getText().isEmpty()||textField_4.getText().isEmpty()||
								textField_5.getText().isEmpty()||textField_6.getText().isEmpty()||textField_7.getText().isEmpty()||textField_8.getText().isEmpty()||
								textField_9.getText().isEmpty()||textField_10.getText().isEmpty()||textField_11.getText().isEmpty()||textField_12.getText().isEmpty())
						{
							JOptionPane.showMessageDialog(SpecificProgramInfo.this,"Incomplete Information");
						}else {
							Program p = new Program();
							p.setCountry(textField_1.getText());
							p.setUniversity(textField_2.getText());
							p.setSchool(textField_3.getText());
							p.setProgramName(textField_4.getText());
							p.setHomepage(textField_5.getText());
							p.setLocation(textField_6.getText());
							p.setEmail(textField_7.getText());
							p.setPhoneNumber(textField_8.getText());
							p.setDegree(textField_9.getText());
							p.setDeadlineWithAid(textField_10.getText());
							p.setDeadlineWithoutAid(textField_11.getText());
							p.setId(textField_12.getText());

							Action.update(p,program);
							
							JOptionPane.showMessageDialog(SpecificProgramInfo.this,"Update Successfully");
						}
					}
				});
				updateButton.setFont(new Font("SimSun", Font.PLAIN, 28));
				updateButton.setBounds(0, 10, 253, 72);
				updateButton.setActionCommand("OK");
				buttonPane.add(updateButton);
				getRootPane().setDefaultButton(updateButton);
			}
			{
				JButton deleteButton = new JButton("Delete");
				deleteButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(textField_1.getText().isEmpty()||textField_2.getText().isEmpty()||textField_3.getText().isEmpty()||textField_4.getText().isEmpty()||
								textField_5.getText().isEmpty()||textField_6.getText().isEmpty()||textField_7.getText().isEmpty()||textField_8.getText().isEmpty()||
								textField_9.getText().isEmpty()||textField_10.getText().isEmpty()||textField_11.getText().isEmpty()||textField_12.getText().isEmpty())
						{
							JOptionPane.showMessageDialog(SpecificProgramInfo.this,"Incomplete Information");
						}else {
							Program p = new Program();
							p.setCountry(textField_1.getText());
							p.setUniversity(textField_2.getText());
							p.setSchool(textField_3.getText());
							p.setProgramName(textField_4.getText());
							p.setHomepage(textField_5.getText());
							p.setLocation(textField_6.getText());
							p.setEmail(textField_7.getText());
							p.setPhoneNumber(textField_8.getText());
							p.setDegree(textField_9.getText());
							p.setDeadlineWithAid(textField_10.getText());
							p.setDeadlineWithoutAid(textField_11.getText());
							p.setId(textField_12.getText());

							Action.delete(p);
							
							JOptionPane.showMessageDialog(SpecificProgramInfo.this,"Delete Successfully");
						}
					}
				});
				deleteButton.setFont(new Font("SimSun", Font.PLAIN, 28));
				deleteButton.setBounds(251, 10, 228, 72);
				deleteButton.setActionCommand("Cancel");
				buttonPane.add(deleteButton);
			}
			
			JButton cancelbutton = new JButton("Cancel");
			cancelbutton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					SpecificProgramInfo.this.dispose();;
				}
			});
			cancelbutton.setFont(new Font("SimSun", Font.PLAIN, 28));
			cancelbutton.setActionCommand("Cancel");
			cancelbutton.setBounds(476, 10, 214, 72);
			buttonPane.add(cancelbutton);
		}
	}
}
